<%@ page pageEncoding="UTF-8"%>
<%@ page import="java.util.Map.Entry,java.util.*,java.util.regex.*,java.net.*,java.io.*,java.text.*"%>
<%@ page import="com.tianxiabuyi.*,com.tianxiabuyi.persist.*,com.tianxiabuyi.model.*,com.tianxiabuyi.utils.*"%>
<%@ page import="org.jsoup.Jsoup,org.jsoup.nodes.Element,org.jsoup.select.Elements"%>
<%@ page import="org.dom4j.*"%>
<%!
	public Integer NEWS_CATEGORY = 1;   // 新闻
	public Integer QUEST_CATEGORY = 2;  // 问答
	public Integer DOCTOR_CATEGORY = 3; // 医生
	public Integer COMMENT_CATEGORY = 4; // 评论
	public Integer STEP_CATEGORY = 5;   // 步数
	public Integer LABEL_CATEGORY = 6;   // 标签
	public Integer DEPT_CATEGORY = 7;   // 科室
	public Integer SECTION_DOCTOR = 8; // 科室版医生
	public Integer SECTION_PATIENT = 9; // 科室版患者
	public Integer SECTION_SECRETARY = 10; // 科室版秘书
	public Integer SEE = 1; // 浏览
	public Integer ATTENTION = 2; // 关注
	public Integer LOVE = 3; // 点赞
	public Integer TREAD = 4; // 踩
	public Integer CANCEL_LOVE = 5; // 取消点赞
	public Integer CANCEL_TRRAD = 6; // 取消踩
	public Integer AUTH_REPORT = 7; // 报告授权
	public Integer NO_DISTURB = 8; // 免打扰
	// 操作记录 cancel为true是取消false为增加
	public Message operate(OperateHistory oh, boolean cancel) {
		List<OperateHistory> ohs = new ArrayList<OperateHistory>();
		Integer id = null;
		if (cancel) {  // 取消收藏、关注
			ohs = QBuilder.db("basedb").select().from("OperateHistory").where(new Where("uid=", oh.getUid()).and("oid=", oh.getOid()).and("category=", oh.getCategory()).and("operate=", oh.getOperate()).and("status=", STATUS_ENABLE).and("hospital=", oh.getHospital())).page(1,1).query().getResultObj();
			if (oh.getId() != null) {
				ohs = QBuilder.db("basedb").select().from("OperateHistory").where(new Where("id=", oh.getId()).and("status=", STATUS_ENABLE).and("hospital=", oh.getHospital())).page(1,1).query().getResultObj();
			}
			if (ohs.size() == 0) {
				return new Message(30091, "没有相关记录");
			}
			oh = ohs.get(0);
			oh.setStatus(STATUS_DISABLE);
			id = oh.getId();
			DBTools.db("basedb").modItem(oh, oh.getId());
		} else {
			if (SEE.equals(oh.getOperate())) {
				id = (Integer)DBTools.db("basedb").addItem(oh);
			} else {
				ohs = QBuilder.db("basedb").select().from("OperateHistory").where(new Where("uid=", oh.getUid()).and("oid=", oh.getOid()).and("category=", oh.getCategory()).and("operate=", oh.getOperate()).and("status=", STATUS_ENABLE).and("hospital=", oh.getHospital())).page(1,1).query().getResultObj();
				if (ohs.size() == 0) {
					id = (Integer)DBTools.db("basedb").addItem(oh);
				} else {
					return new Message(30091, "该操作不能重复执行");
				}
			}
		}
		Message msg = new Message();
		Map dataMap = new HashMap();
		dataMap.put("id", id);
		msg.put("data", dataMap);
		return msg;
	}

	// 查询某个用户浏览次数、关注次数、点赞次数、踩次数
	public Map query(Map map, OperateHistory oh) {
		map.put("browse", 0);
		map.put("attention", 0);
		map.put("love", 0);
		map.put("tread", 0);
		List<Map> counts = QBuilder.db("basedb").select("new map(oid as oid, operate as operate, uid as uid)").from("OperateHistory").where(new Where("oid=", oh.getOid()).and("uid=", oh.getUid()).and("status=", STATUS_ENABLE).and("hospital=", oh.getHospital()).and("category=", oh.getCategory())).query().getResultObj();
		for (int i = 0; i < counts.size(); i++) {
			Map _map = counts.get(i);
			Integer operate = Tools.getInt(_map.get("operate") + "", -1);
			if (operate == -1) continue;
			String key = operate.equals(SEE) ? "browse" : operate.equals(ATTENTION) ? "attention" :operate.equals(LOVE) ?  "love":operate.equals(TREAD) ? "tread" : "";
			if ("".equals(key)) continue;
			map.put(key, 1);
		}
		return map;
	}
	
	// 完善浏览量，点赞量 ，是否关注,批量
	void completeOperateCount(List<Map> list, Integer category, Integer hospital, Integer uid) {
		OperateHistory oh = new OperateHistory();
		oh.setHospital(hospital);
		oh.setCategory(category);
		love(query(list, oh), uid, oh);
	}
	
	// 完善浏览量，点赞量 ，是否关注，单条
	void completeOperateCount(Map map, Integer category, Integer hospital, Integer uid) {
		OperateHistory oh = new OperateHistory();
		oh.setHospital(hospital);
		oh.setCategory(category);
		List<Map> list = new ArrayList<Map>();
		list.add(map);
		love(query(list, oh), uid, oh);
	}
	
	// 查询浏览次数、关注次数、点赞次数、踩次数
	public List query(List list, OperateHistory oh) {
		String ids = "'-9999999'";
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			String id = Tools.getDesc(map.get("id"), "");
			if ("".equals(id)) continue;
			ids += ",'" + id + "'";
		}
		List counts = QBuilder.db("basedb").select("new map(count(id) as count, oid as oid, operate as operate)").from("OperateHistory").where(new Where("oid in(" + ids +")").and("category=", oh.getCategory()).and("status=", STATUS_ENABLE).and("hospital=", oh.getHospital())).group("oid, operate").query().getResultObj();
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			map.put("browse", 0);
			map.put("attention", 0);
			map.put("love", 0);
			map.put("tread", 0);
			String id = Tools.getDesc(map.get("id"), "");
			Iterator it = counts.iterator();
			while (it.hasNext()) {
				Map temp = (Map)it.next();
				String tempId = Tools.getDesc(temp.get("oid"), "----");
				if (id.equals(tempId)) {
					Integer operate = Tools.getInt(temp.get("operate") + "", -1);
					if (operate == -1) continue;
					String key = operate.equals(SEE) ? "browse" : operate.equals(ATTENTION) ? "attention" :operate.equals(LOVE) ?  "love":"tread";
					map.put(key, Tools.getLong(temp.get("count") + "", 0L));
					it.remove();
				}
			}
		}
		return list;
	}

	//  查询是否已经赞过、踩过或关注过(对新闻、问题或者评论)
	public List<Map> love(List<Map> list, Integer uid, OperateHistory oh) {
		Map<Integer, String[]> opMap = new HashMap<Integer, String[]>();
		opMap.put(TREAD, new String[]{"is_treaded", "treaded_id"});
		opMap.put(LOVE, new String[]{"is_loved", "loved_id"});
		opMap.put(ATTENTION, new String[]{"is_attentioned", "attentioned_id"});
		Set<Integer> set = opMap.keySet();
		for (Integer key:set) {
			List<Map> oo = QBuilder.db("basedb").select("new map(oid as oid,id as id)").from("OperateHistory").where(new Where("uid=", uid).and("category=", oh.getCategory()).and("status=", STATUS_ENABLE).and("operate=", key)).query().getResultObj();
			List<String> oids = new ArrayList<String>();
			Map<String, Integer> idMap = new HashMap<String, Integer>();
			for (Map map:oo) {
				oids.add((String)map.get("oid"));
				idMap.put(map.get("oid") + "", (Integer)map.get("id"));
			}
			for (Map map:list) {
				String id = Tools.getDesc(map.get("id"));
				if (oids.contains(id)) {
					map.put(opMap.get(key)[0], 1);
					map.put(opMap.get(key)[1], idMap.get(id));
				}
				else
					map.put(opMap.get(key)[0], 0);
			}
		}
		return list;
	}
%>
